package cn.antblog.jdk8.time;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.Month;
import java.time.format.DateTimeFormatter;

public class TimeTest {


    public static void main(String args[]){
        TimeTest timeTest = new TimeTest();
        timeTest.testLocalDateTime();

    }

    public void testLocalDateTime(){
        //获取当前的日期时间
        LocalDateTime currentTime = LocalDateTime.now();
        System.out.println("当前时间："+currentTime);
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        System.out.println("当前时间格式化后："+currentTime.format(formatter));

        LocalDate date1 = currentTime.toLocalDate();
        System.out.println("date1："+date1);
        LocalTime date2 = currentTime.toLocalTime();
        System.out.println("date2:"+date2);

        Month month = currentTime.getMonth();
        int monthValue = month.getValue();
        int day = currentTime.getDayOfMonth();
        int seconds = currentTime.getSecond();
        int year = currentTime.getYear();

        System.out.println("年:"+year+", 月: " + monthValue +", 日: " + day +", 秒: " + seconds);

        LocalDateTime date3 = currentTime.withDayOfMonth(10).withYear(2012);
        System.out.println("date3:"+date3);

        // 12 december 2014
        LocalDate date4 = LocalDate.of(2022, Month.DECEMBER, 12);
        System.out.println("date4:"+date4);

        // 22 小时 15 分钟
        LocalTime date5 = LocalTime.of(22, 15);
        System.out.println("date5:"+date5);

        // 解析字符串
        LocalTime date6 = LocalTime.parse("20:15:30");
        System.out.println("date6:"+date6);

        LocalDate date133 = currentTime.toLocalDate();
        System.out.println(date133);
    }
}
